﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ShellCommandClientService.PushShellCommandServiceReference;
using ShellCommandClientService.ShellCommandResultLoggerServiceReference;

namespace ShellCommandClientService
{
    public class ShellCommandExecuter : IShellCommandExecuter
    {
        public string Execute(ShellCommandInfo shellCommandInfo, out bool isShellCommandRunSuccess)
        {
            Process cmd = new Process();
            string result = string.Empty;
            isShellCommandRunSuccess = false;
            try
            {
                cmd.StartInfo.FileName = "cmd.exe";
                cmd.StartInfo.RedirectStandardInput = true;
                cmd.StartInfo.RedirectStandardOutput = true;
                cmd.StartInfo.CreateNoWindow = true;
                cmd.StartInfo.UseShellExecute = false;
                cmd.Start();
                cmd.StandardInput.WriteLine(shellCommandInfo.Text);
                cmd.StandardInput.Flush();
                cmd.StandardInput.Close();

                result = cmd.StandardOutput.ReadToEnd();
                isShellCommandRunSuccess = true;
            }
            catch (Exception ex)
            {
                result = ex.Message + " " + ex.Source;
            }
            return result;
        }
    }
}
